package com.lsfb.signln.Location.LocationSearch.ReplenishSignIn;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.lsfb.signln.Login.LoginActivity;
import com.lsfb.signln.R;
import com.lsfb.signln.Utils.LittleUtils;
import com.lsfb.signln.Utils.URLString;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.T;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@ContentView(R.layout.aty_hyx_search_address)
public class SearchAddressActivity extends LsfbActivity implements SearchAddressAdapter.ItemClickListener {

    /**
     * 地址输入框
     */
    @ViewInject(R.id.et_address)
    private EditText et_address;

    /**
     * 地址搜索结果列表
     */
    @ViewInject(R.id.rv_address)
    private RecyclerView rv_address;
    private List<SearchCityBean.ListBean> address;
    private SearchAddressAdapter adapter;

    /**
     * 返回Activity的请求码
     */
    private int requestCode = 1;
    private String word;

    @Override
    public void initView() {
        LittleUtils.setsimpletitlebar(this, "出差地址选择");
        et_address.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER) {
                    // 先隐藏键盘
                    ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(SearchAddressActivity.this.getCurrentFocus()
                                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    //进行搜索操作的方法，在该方法中可以加入mEditSearchUser的非空判断
                    if (TextUtils.isEmpty(et_address.getText().toString().trim())) {
                        T.showShort(getApplicationContext(), "请输入出差地址!");
                    } else {
                        getSearchData(et_address.getText().toString().trim());
                    }
                }
                return false;
            }
        });

        address = new ArrayList<>();
        adapter = new SearchAddressAdapter(this, address, R.layout.item_hyx_address, this);
        rv_address.setLayoutManager(new LinearLayoutManager(this));
        rv_address.setAdapter(adapter);

    }

    /**
     * 获取搜索列表数据
     */
    private void getSearchData(String cityName) {
        HashMap hashMap = new HashMap();
        hashMap.put("cityName", cityName);
        new BaseInternet().getData_Get(URLString.GETCITYS, hashMap, SearchCityBean.class, new SearchCityEvent<SearchCityBean>(), true, false);
    }

    @EventAnnotation
    public void eventResult(SearchCityEvent<SearchCityBean> event) {
        switch (event.getCode()) {
            case 200:
                if (event.getData().getList() != null && !event.getData().getList().isEmpty()) {
                    address.clear();
                    address.addAll(event.getData().getList());
                    adapter.notifyDataSetChanged();
                } else {
                    address.clear();
                    adapter.notifyDataSetChanged();
                }
                break;
            case 1006:
                T.showShort(this, "您的账号已在其它设备登录，请重登录");
                LittleUtils.exit(this);
                startActivity(new Intent(this, LoginActivity.class));
                break;
            default:
                T.showShort(this, event.getMsg());
                break;
        }
    }

    @Override
    public void initEvent() {

    }

    @OnClick({R.id.aty_img_search})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.aty_img_search:
                if (TextUtils.isEmpty(et_address.getText().toString().trim())) {
                    T.showShort(getApplicationContext(), "请输入出差地址!");
                } else {
                    getSearchData(et_address.getText().toString().trim());
                }
                break;
        }
    }

    @Override
    public void initData() {

        getSearchData(word);
    }

    /**
     * 原因列表点击事件
     *
     * @param model
     */
    @Override
    public void onClick(SearchCityBean.ListBean model) {
        Intent intent = new Intent();
        intent.putExtra("datas", model);
        setResult(requestCode, intent);
        this.finish();
    }
}
